<template>
	<view class="container">
		<uni-forms ref="form" :model="user" :rules="rules">
			<uni-forms-item label="用户名" name="username">
				<uni-easyinput v-model="user.username" placeholder="请输入用户名" />
			</uni-forms-item>

			<uni-forms-item label="密码" name="password">
				<uni-easyinput type="password" v-model="user.password" placeholder="请输入密码" />
			</uni-forms-item>

			<uni-forms-item label="号码" name="phonenumber">
				<uni-easyinput v-model="user.phonenumber" placeholder="请输入号码" />
			</uni-forms-item>

			<uni-forms-item label="姓名" name="name">
				<uni-easyinput v-model="user.name" placeholder="请输入姓名" />
			</uni-forms-item>

			<uni-forms-item label="邮箱" name="email">
				<uni-easyinput v-model="user.email" placeholder="请输入邮箱" />
			</uni-forms-item>

			<uni-forms-item label="学院" name="college">
				<picker mode="selector" :range="college" @change="collegeChange" style="padding-top: 7px;">
					<view v-if="index>=0">{{college[index]}}</view>
					<view v-else>请选择你的学院</view>
				</picker>
			</uni-forms-item>

			<uni-forms-item label="专业" name="profession">
				<picker mode="selector" :range="profession" @change="pickerChange" style="padding-top: 7px;">
					<view required v-if="indexs>=0">{{profession[indexs]}}</view>
					<view v-else>请选择你的专业</view>
				</picker>
			</uni-forms-item>

			<uni-forms-item label="">
				<button type="primary" @click="register">注册</button>
			</uni-forms-item>
		</uni-forms>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				user: {
					username: '',
					password: '',
					phonenumber: '',
					name: ''
				},
				index: -1,
				indexs: -1,
				college: ['信息工程学院', '经济管理学院', '智能制造学院', '文法学院'],
				profession: ['移动应用开发', '计算机网络技术', '数字媒体', '软件工程'],
				rules: {
					username: {
						rules: [{
							required: true,
							errorMessage: '请输入用户名',
						}]
					},
					password: {
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
							{
								minLength: 3,
								maxLength: 5,
								errorMessage: '密码长度必须 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
					email: {
						rules: [{
							required: true,
							errorMessage: '请输入邮箱'
						}, {
							format: 'email',
							errorMessage: '邮箱格式错误'
						}]
					},
					phonenumber: {
						rules: [{
								required: true,
								errorMessage: '请输入号码'
							}, {
								minLength: 11,
								maxLength: 11,
								errorMessage: '{label}长度必须 {maxLength} 个字符',
							},
							{
								validateFunction: function(rule, value, data, callback) {
									var phoneRegex =
										/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
									if (!phoneRegex.test(value)) {
										callback('号码格式错误');
									}
									return true
								}
							}
						]
					},
					name: {
						rules: [{
							required: true,
							errorMessage: '请输入姓名',
						}]
					},
				},
			}
		},
		onReady() {
			// 需要在onReady中设置规则
			this.$refs.form.setRules(this.rules)
		},
		methods: {
			collegeChange(e) {
				this.index = e.detail.value
			},
			pickerChange(e) {
				this.indexs = e.detail.value
			},

			register(e) {
				uni.showLoading({
					title: '注册中...'
				});

				this.$refs.form.validate().then(valid => {
					if (valid) {
						uni.hideLoading();
						uni.request({
							url: 'http://localhost:8090/freshmenapp/user/register',
							method: 'POST',
							data: this.user,
							success: (res) => {
								if (res.data.success) {
									uni.showToast({
										title: this.user.name + '注册成功',
										icon: 'success'
									});
								} else {
									uni.showToast({
										title: '注册失败',
										icon: 'error'
									});
								}
							},
							fail: () => {
								uni.showToast({
									title: '注册失败，检查网络',
									icon: 'error'
								});
							},
							complete: () => {}
						});
					} else {
						uni.hideLoading();
					}
				}).catch(err => {
					uni.hideLoading();
					console.error('表单验证出错：', err);
				});
			},

			onLoad: () => {
				uni.onTabBarMidButtonTap(function(e) {
					console.log(e)

				})
			}
		},
	}
</script>

<style>
	.container {
		padding: 20px;
		font-size: 14px;
		line-height: 24px;
	}
</style>